package com.zheng.damo1.电商系统;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Scanner;

public class CommodityInterFaceImpe implements CommodityInterFace{
    public static final ArrayList<Commodity> LISTS=new ArrayList<>();
    public static final ArrayList<Commodity> sumLISTS=new ArrayList<>();
    public static final Scanner INPUT=new Scanner(System.in);
    static {
        LISTS.add(new Commodity("10101",ComNane.海尔冰箱.name(), 3999.9,50));
        LISTS.add(new Commodity("10102",ComNane.格力冰箱.name(),2888.8,30));
        LISTS.add(new Commodity("10103",ComNane.TCL冰箱.name(),1999.8,100));
        LISTS.add(new Commodity("10104",ComNane.美的冰箱.name(),4999.9,60));
        LISTS.add(new Commodity("10201",ComNane.海尔空调.name(),3099.9,50));
        LISTS.add(new Commodity("10202",ComNane.格力空调.name(),2808.8,40));
        LISTS.add(new Commodity("10203",ComNane.TCL空调.name(),1099.8,120));
        LISTS.add(new Commodity("10204",ComNane.美的空调.name(),4909.9,65));
    }
    enum ComNane{
        海尔冰箱,
        格力冰箱,
        TCL冰箱,
        美的冰箱,
        海尔空调,
        格力空调,
        TCL空调,
        美的空调,
    }
    @Override
    public void CommodityShow() {
        out:
        while (true){
            System.out.println("**************电器商城***************");
            System.out.println("             1.商品购买");
            System.out.println("             2.我的购物车");
            System.out.println("             3.个人信息");
            System.out.println("             4.注销息");
            System.out.println("**************电器商城***************");
            System.out.println("请输入菜单");
            int count =Integer.parseInt(INPUT.nextLine());
            switch (count){
                case 1:
                    //商品购买
                    purchaseComm();
                    break ;
                case 2:
                    //我的购物车
                    CommodityCar();
                    break;
                case 3:
                    //个人信息
                    UserData();
                    break;
                case 4:
                    break out;
                default:
                    System.out.println("数输入错误");
                    break;
            }
        }

    }

    @Override
    public void purchaseComm() {
        while (true) {
            boolean b=false;
            boolean b1=false;
            for (Commodity list : LISTS) {
                System.out.println(list);
            }
            System.out.println("请输入商品编号");
            String id=INPUT.nextLine();
            for (Commodity list : LISTS) {
                if (list.getId().equals(id)){
                    b1=true;
                    while (true){
                        System.out.println("请输入要购买的数量");
                        int count1=Integer.parseInt(INPUT.nextLine());
                        if (count1<=0){
                            System.out.println("购买的数量不为零或负数");
                        }else if (count1>list.getNumber()){
                            System.out.println("购买的数量必须小于"+list.getNumber());
                        }else {
                            list.setNumber(list.getNumber()-count1);
                            sumLISTS.add(new Commodity(list.getId(),list.getName(), list.getMoney(), count1));
                            b=true;
                            break;

                        }
                    }
                }
            }
            if (b) {
                System.out.println("你还要继续购买吗？（是Y  否 任意键）");
                String str=INPUT.nextLine();
                if (!"Y".equals(str)){
                    System.out.println("请到购物车付款！");
                    break;
                }
            }
            if (!b1){
                System.out.println("您输入的商品单号不存在!!!");
            }

        }

    }

    @Override
    public void CommodityCar() {
        for (Commodity sumLIST : sumLISTS) {
            System.out.println(sumLIST);
        }
        BigDecimal bd=new BigDecimal("0.0");
        for (Commodity sumLIST : sumLISTS) {
            bd=bd.add(new BigDecimal(sumLIST.getMoney()+"").multiply(new BigDecimal(sumLIST.getNumber()+"")));
        }
        System.out.println("总金额为："+bd);
        if (sumLISTS.size()!=0){
            System.out.println("您是否付款？（是Y  否 任意键）");
            String str=INPUT.nextLine();
            if ("Y".equals(str)){
                while (true) {
                    System.out.println("请输入付款金额：");
                    int numbers=Integer.parseInt(INPUT.nextLine());
                    if (numbers<bd.doubleValue()){
                        System.out.println("金额不足");
                    }else {
                        bd=new BigDecimal(numbers+"").subtract(bd);
                        System.out.println("找零："+bd);
                        sumLISTS.clear();
                        break;
                    }
                }
            }
        }else {
            System.out.println("您还未添加商品");
        }

    }

    @Override
    public void UserData() {
        System.out.println("登录名："+LoginImpl.USER.getUserName());
        System.out.println("年龄："+LoginImpl.USER.getAge());
        System.out.println("昵称："+LoginImpl.USER.getOtherName());
    }

}
